﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lg.Core.Extensions
{
    public static class StringExtensions
    {
        public static bool ToBoolean(this string i, bool b)
        {
            try { b = Convert.ToBoolean(i); } catch { }
            return b;
        }

        /// <summary>
        /// 将JSON格式的字符串反序列化为实体model
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="str">JSON格式字符串</param>
        /// <returns>Model</returns>
        public static T? ToModel<T>(this string str) where T : class
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(str);
            }
            catch (Exception e)
            {
                return null;
            }
        }
        /// <summary>
        /// 将JSON数组格式字符串反序列化为List
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="str">JSON数组格式字符</param>
        /// <returns>List</returns>
        public static List<T>? ToList<T>(this string str)
        {
            try
            {
                return JsonConvert.DeserializeObject<List<T>>(str);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 将Json字符串转换为Json对像
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static JObject ToJobject(this string str)
        {
            try
            {
                return JObject.Parse(str);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }       
        /// <summary>
        /// 较验字符串数组中的元素是否都是数字
        /// 常用于前端以字符串形式传递多个ID时的较验
        /// </summary>
        /// <param name="strs">字符串数组</param>
        /// <returns></returns>

        public static bool IsInt(this string[] strs)
        {
            bool b = true;
            foreach (string s in strs)
            {
                try
                {
                    Convert.ToInt32(s);
                }
                catch
                {
                    //throw new Exception(e.Message);
                    b = false;
                }
            }
            return b;
        }
        public static bool IsInt(this string[] strs,out int[]? intArry)
        {
            bool b = true;
            List<int> list = new List<int>();
            foreach (string s in strs)
            {
                try
                {
                   list.Add(Convert.ToInt32(s));
                }
                catch
                {
                    //throw new Exception(e.Message);
                    b = false;
                }
            }
            if (b)
            {
                intArry = new int[list.Count];
            }
            else
            {
                intArry=null;
            }            
            return b;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="keys"></param>
        /// <returns></returns>

        public static bool XSSCheck(this string str,string keys = "script,alert,eval,onerror,onload")
        {
            List<string> listKeys = keys.Split(",").ToList();
            foreach (string key in listKeys)
            {
                if (str.Contains(key))
                {
                    return false;
                }
            }
            return true;
        }
    }
}
